"""
CPEM Secure XML RPC Request Handler (lib/SecureXMLRpcRequestHandler.py)   

These bits were taken from http://code.activestate.com/recipes/496786
Some modification by: C.J. Steele <coreyjsteele@gmail.com>.
"""

import SocketServer
import BaseHTTPServer
import SimpleHTTPServer
import SimpleXMLRPCServer
import inspect
import socket,os
from OpenSSL import SSL

class SecureXMLRpcRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
    def setup(self):
        self.connection = self.request
        self.rfile = socket._fileobject(self.request,"rb",self.rbufsize)
        self.wfile = socket._fileobject(self.request,"wb",self.wbufsize)

    def do_POST(self):
        try:
            data = self.rfile.read(int(self.headers["content-length"]))
            response = self.server._marshaled_dispatch(data,getattr(self,'_dispatch',None))
        except: # This should only happen if the module is buggy
            self.send_response(500)
            self.end_headers()
        else:
            self.send_response(200)
            self.send_header("Content-type","text/xml")
            self.send_header("Content-length",str(len(response)))
            self.end_headers()
            self.wfile.write(response)

            # shut down the connection
            self.wfile.flush()
            self.connection.shutdown()
